/**
 * @author   zhangyp
 * @created  2013-10-14 下午12:05:41
 * @email    zviolet@163.com
 * TODO      开机启动的service
 */
package com.bible.fire.service;

import com.bible.fire.MainActivity;
import com.bible.fire.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

public class BootService extends Service {
	
	private static String TAG = BootService.class.getSimpleName();
	
	//获取消息线程 
    private MessageThread messageThread = null; 

    //点击查看 
    private Intent messageIntent = null; 
    private PendingIntent messagePendingIntent = null; 

    //通知栏消息 
    private int messageNotificationID = 1000; 
    private Notification messageNotification = null; 
    private NotificationManager messageNotificationManager = null; 

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	public void onCreate(){
		Log.d(TAG, "create boot service");
		super.onCreate();
	}
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d(TAG, "start boot service");
		//初始化 
        messageNotification = new Notification(); 
        messageNotification.icon = R.drawable.ic_launcher;  //通知图片 
        messageNotification.tickerText = "新消息";         //通知标题 
        //messageNotification.defaults = Notification.DEFAULT_SOUND; 
        messageNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

        //点击查看 
        //messageIntent = new Intent(this,MainActivity.class); 
        messageIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS );
        messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0); 

        //开启线程 
        MessageThread thread = new MessageThread(); 
        thread.isRunning = true; 
        thread.start();
		return START_STICKY;
	}
	
	class MessageThread extends Thread{ 
        //运行状态 
        public boolean isRunning = true; 

        @Override 
        public void run() { 
            while(isRunning){ 
                try { 
                    //休息30秒 
                    Thread.sleep(30000); 
                    if(getServerMessage().equals("yes")){ 
                        //设置消息内容和标题 
                        messageNotification.setLatestEventInfo(BootService.this, "您有新消息!", "这是一条新的测试消息", messagePendingIntent); 
                        //发布消息 
                        messageNotificationManager.notify(messageNotificationID, messageNotification); 
                        //避免覆盖消息，采取ID自增 
                        messageNotificationID++; 
                    } 
                } catch (Exception e) { 
                    Log.e(TAG, "publish message error", e);
                } 
            }    
        } 

    } 

    /***
     * 模拟了服务端的消息。实际应用中应该去服务器拿到message
     * @return
     */ 
    public String getServerMessage(){ 
        return "yes"; 
    } 

}
